package com.android.lotto.fragment.union;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lotto.R;
import com.android.lotto.fragment.BaseFragment;
import com.android.lotto.widget.PickerEditView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Created by JS-00 on 2017/5/19.
 */

public class UnionRadomFragment extends BaseFragment implements View.OnClickListener {
    private TextView mUnionLottoResult, mUnionLottoCheck;
    private PickerEditView mRedNumEdit, mBlueNumEdit;
    private Button mRandomBtn, mSaveListBtn;
    private CheckBox firstRed, secondRed, thirdRed, fourRed, fiveRed, sixRed, sevenRed, eightRed,
            nineRed, tenRed, elevenRed, twelveRed, thirteenRed, fourteenRed, fifteenRed, sixteenRed,
            seventeenRed, eighteenRed, ninteenRed, twentyRed, twentyoneRed, twentytwoRed, twentythreeRed,
            twentyfourRed, twentyfiveRed, twentysixRed, twentysevenRed, twentyeightRed, twentynineRed,
            thirtyRed, thirtyoneRed, thirtytwoRed, thirtythreeRed;
    private CheckBox firstBlue, secondBlue, thirdBlue, fourBlue, fiveBlue, sixBlue, sevenBlue, eightBlue,
            nineBlue, tenBlue, elevenBlue, twelveBlue, thirteenBlue, fourteenBlue, fifteenBlue, sixteenBlue;
    private List<CheckBox> mRedList, mBlueList;
    private Context mContext;
    private View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = inflater.inflate(R.layout.union_random_forecast, null);
        mContext = getActivity();
        mUnionLottoResult = (TextView) mView.findViewById(R.id.unionLottoPreResult);
        mUnionLottoCheck = (TextView) mView.findViewById(R.id.unionLottoCheck);
        mRandomBtn = (Button) mView.findViewById(R.id.randomBtn);
        mRandomBtn.setText(mContext.getResources().getString(R.string.text_lotto_choose_random));
        mRandomBtn.setOnClickListener(this);
        mSaveListBtn = (Button) mView.findViewById(R.id.saveListBtn);
        mSaveListBtn.setOnClickListener(this);
        mUnionLottoResult.setText(mContext.getString(R.string.text_lotto_preResult));
        initRedBalls();
        initBlueBalls();
        mRedNumEdit = (PickerEditView) mView.findViewById(R.id.modifyRedNumValue);
        mRedNumEdit.setText("6");
        mRedNumEdit.setOnOperationListener(mRedOnOperationListener);
        mBlueNumEdit = (PickerEditView) mView.findViewById(R.id.modifyBlueNumValue);
        mBlueNumEdit.setText("1");
        mBlueNumEdit.setOnOperationListener(mBlueOnOperationListener);
        return mView;
    }

    PickerEditView.OnOperationListener mRedOnOperationListener = new PickerEditView.OnOperationListener(){
        @Override
        public void onOperation(int inc) {
            if (inc > 0) {
                if (Integer.parseInt(mRedNumEdit.getText()) < 33) {
                    mRedNumEdit.setText(""+(Integer.parseInt(mRedNumEdit.getText())+1));
                }
            } else {
                if (Integer.parseInt(mRedNumEdit.getText()) > 6) {
                    mRedNumEdit.setText(""+(Integer.parseInt(mRedNumEdit.getText())-1));
                }
            }
        }
    };

    PickerEditView.OnOperationListener mBlueOnOperationListener = new PickerEditView.OnOperationListener(){
        @Override
        public void onOperation(int inc) {
            if (inc > 0) {
                if (Integer.parseInt(mBlueNumEdit.getText()) < 16) {
                    mBlueNumEdit.setText(""+(Integer.parseInt(mBlueNumEdit.getText())+1));
                }
            } else {
                if (Integer.parseInt(mBlueNumEdit.getText()) > 1) {
                    mBlueNumEdit.setText(""+(Integer.parseInt(mBlueNumEdit.getText())-1));
                }
            }
        }
    };

    private void initRedBalls() {
        mRedList = new ArrayList<CheckBox>();
        firstRed = (CheckBox) mView.findViewById(R.id.firstRed);
        mRedList.add(firstRed);
        secondRed = (CheckBox) mView.findViewById(R.id.secondRed);
        mRedList.add(secondRed);
        thirdRed = (CheckBox) mView.findViewById(R.id.thirdRed);
        mRedList.add(thirdRed);
        fourRed = (CheckBox) mView.findViewById(R.id.fourRed);
        mRedList.add(fourRed);
        fiveRed = (CheckBox) mView.findViewById(R.id.fiveRed);
        mRedList.add(fiveRed);
        sixRed = (CheckBox) mView.findViewById(R.id.sixRed);
        mRedList.add(sixRed);
        sevenRed = (CheckBox) mView.findViewById(R.id.sevenRed);
        mRedList.add(sevenRed);
        eightRed = (CheckBox) mView.findViewById(R.id.eightRed);
        mRedList.add(eightRed);
        nineRed = (CheckBox) mView.findViewById(R.id.nineRed);
        mRedList.add(nineRed);
        tenRed = (CheckBox) mView.findViewById(R.id.tenRed);
        mRedList.add(tenRed);
        elevenRed = (CheckBox) mView.findViewById(R.id.elevenRed);
        mRedList.add(elevenRed);
        twelveRed = (CheckBox) mView.findViewById(R.id.twelveRed);
        mRedList.add(twelveRed);
        thirteenRed = (CheckBox) mView.findViewById(R.id.thirteenRed);
        mRedList.add(thirteenRed);
        fourteenRed = (CheckBox) mView.findViewById(R.id.fourteenRed);
        mRedList.add(fourteenRed);
        fifteenRed = (CheckBox) mView.findViewById(R.id.fifteenRed);
        mRedList.add(fifteenRed);
        sixteenRed = (CheckBox) mView.findViewById(R.id.sixteenRed);
        mRedList.add(sixteenRed);
        seventeenRed = (CheckBox) mView.findViewById(R.id.seventeenRed);
        mRedList.add(seventeenRed);
        eighteenRed = (CheckBox) mView.findViewById(R.id.eighteenRed);
        mRedList.add(eighteenRed);
        ninteenRed = (CheckBox) mView.findViewById(R.id.ninteenRed);
        mRedList.add(ninteenRed);
        twentyRed = (CheckBox) mView.findViewById(R.id.twentyRed);
        mRedList.add(twentyRed);
        twentyoneRed = (CheckBox) mView.findViewById(R.id.twentyoneRed);
        mRedList.add(twentyoneRed);
        twentytwoRed = (CheckBox) mView.findViewById(R.id.twentytwoRed);
        mRedList.add(twentytwoRed);
        twentythreeRed = (CheckBox) mView.findViewById(R.id.twentythreeRed);
        mRedList.add(twentythreeRed);
        twentyfourRed = (CheckBox) mView.findViewById(R.id.twentyfourRed);
        mRedList.add(twentyfourRed);
        twentyfiveRed = (CheckBox) mView.findViewById(R.id.twentyfiveRed);
        mRedList.add(twentyfiveRed);
        twentysixRed = (CheckBox) mView.findViewById(R.id.twentysixRed);
        mRedList.add(twentysixRed);
        twentysevenRed = (CheckBox) mView.findViewById(R.id.twentysevenRed);
        mRedList.add(twentysevenRed);
        twentyeightRed = (CheckBox) mView.findViewById(R.id.twentyeightRed);
        mRedList.add(twentyeightRed);
        twentynineRed = (CheckBox) mView.findViewById(R.id.twentynineRed);
        mRedList.add(twentynineRed);
        thirtyRed = (CheckBox) mView.findViewById(R.id.thirtyRed);
        mRedList.add(thirtyRed);
        thirtyoneRed = (CheckBox) mView.findViewById(R.id.thirtyoneRed);
        mRedList.add(thirtyoneRed);
        thirtytwoRed = (CheckBox) mView.findViewById(R.id.thirtytwoRed);
        mRedList.add(thirtytwoRed);
        thirtythreeRed = (CheckBox) mView.findViewById(R.id.thirtythreeRed);
        mRedList.add(thirtythreeRed);
    }

    private void initBlueBalls() {
        mBlueList = new ArrayList<CheckBox>();
        firstBlue = (CheckBox) mView.findViewById(R.id.firstBlue);
        mBlueList.add(firstBlue);
        secondBlue = (CheckBox) mView.findViewById(R.id.secondBlue);
        mBlueList.add(secondBlue);
        thirdBlue = (CheckBox) mView.findViewById(R.id.thirdBlue);
        mBlueList.add(thirdBlue);
        fourBlue = (CheckBox) mView.findViewById(R.id.fourBlue);
        mBlueList.add(fourBlue);
        fiveBlue = (CheckBox) mView.findViewById(R.id.fiveBlue);
        mBlueList.add(fiveBlue);
        sixBlue = (CheckBox) mView.findViewById(R.id.sixBlue);
        mBlueList.add(sixBlue);
        sevenBlue = (CheckBox) mView.findViewById(R.id.sevenBlue);
        mBlueList.add(sevenBlue);
        eightBlue = (CheckBox) mView.findViewById(R.id.eightBlue);
        mBlueList.add(eightBlue);
        nineBlue = (CheckBox) mView.findViewById(R.id.nineBlue);
        mBlueList.add(nineBlue);
        tenBlue = (CheckBox) mView.findViewById(R.id.tenBlue);
        mBlueList.add(tenBlue);
        elevenBlue = (CheckBox) mView.findViewById(R.id.elevenBlue);
        mBlueList.add(elevenBlue);
        twelveBlue = (CheckBox) mView.findViewById(R.id.twelveBlue);
        mBlueList.add(twelveBlue);
        thirteenBlue = (CheckBox) mView.findViewById(R.id.thirteenBlue);
        mBlueList.add(thirteenBlue);
        fourteenBlue = (CheckBox) mView.findViewById(R.id.fourteenBlue);
        mBlueList.add(fourteenBlue);
        fifteenBlue = (CheckBox) mView.findViewById(R.id.fifteenBlue);
        mBlueList.add(fifteenBlue);
        sixteenBlue = (CheckBox) mView.findViewById(R.id.sixteenBlue);
        mBlueList.add(sixteenBlue);
    }

    private void removeBack() {
        for (CheckBox checkbox : mRedList) {
            if(checkbox.isChecked()) {
                checkbox.setChecked(false);
            }
        }
        for (CheckBox checkbox : mBlueList) {
            if(checkbox.isChecked()) {
                checkbox.setChecked(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
            case R.id.randomBtn:
                removeBack();
                mUnionLottoCheck.setText(mContext.getString(R.string.text_lotto_check_code)+randomTicket());
                for (CheckBox checkbox : mRedList) {
                    for (int i=0; i<redBall.length; i++)
                        if (Integer.parseInt(checkbox.getText().toString()) == redBall[i]){
                            //checkbox.setBackgroundResource(R.drawable.ball_red);
                            checkbox.setChecked(true);
                        }
                }
                for (CheckBox checkbox : mBlueList) {
                    for (int i=0; i<blueBall.length; i++)
                        if (Integer.parseInt(checkbox.getText().toString()) == blueBall[i]){
                            //checkbox.setBackgroundResource(R.drawable.ball_blue);
                            checkbox.setChecked(true);
                        }
                }
                break;
            case R.id.saveListBtn:
                break;
        }
    }

    /**
     * 蓝球的可选范围
     */
    private static final int[] BLUEBALL = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
            12, 13, 14, 15, 16 };
    /**
     * 红球的可选范围
     */
    private static final int[] REDBALL = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
            12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
            29, 30, 31, 32, 33 };

    /**
     * 保存蓝球
     */
    private int[] blueBall = new int[1];

    /**
     * 保存6红球
     */
    private int[] redBall = new int[6];

    /**
     * 生成一组彩票,一个蓝球 6个红球
     *
     * @return
     */
    public String randomTicket() {
        randomBlueBall();
        randomRedBall();

        final StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < redBall.length; i++) {
            if (i == redBall.length - 1)
                buffer.append(redBall[i]);
            else
                buffer.append(redBall[i] + "\t");
        }
        for (int i = 0; i < blueBall.length; i++) {
            if (i == 0) {
                buffer.append("|" + blueBall[i] + "\t");
            } else if (i == blueBall.length - 1)
                buffer.append(blueBall[i]);
            else
                buffer.append(blueBall[i] + "\t");
        }
        return buffer.toString();
    }

    public String randomLotto() {
        List red = new ArrayList();
        List blue = new ArrayList();
        for (CheckBox checkbox : mRedList) {
            if (checkbox.isChecked()){
                red.add(checkbox.getText());
            }
        }
        if (red.size() < 6) {
            Toast.makeText(mContext, "请至少选择六个红球", Toast.LENGTH_SHORT).show();
            return null;
        }
        for (CheckBox checkbox : mBlueList) {
            if (checkbox.isChecked()){
                blue.add(checkbox.getText());
            }
        }
        if (blue.size() < 1) {
            Toast.makeText(mContext, "请至少选择一个蓝球", Toast.LENGTH_SHORT).show();
            return null;
        }
        final StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < red.size(); i++) {
            if (i == red.size() - 1)
                buffer.append(red.get(i));
            else
                buffer.append(red.get(i) + ",");
        }
        for (int i = 0; i < blue.size(); i++) {
            if (blue.size() > 1 && i == 0) {
                buffer.append("+" + blue.get(i) + ",");
            } else if (blue.size() == 1 && i == 0) {
                buffer.append("+" + blue.get(i));
            }else if (i == blue.size() - 1) {
                buffer.append(blue.get(i));
            } else {
                buffer.append(blue.get(i) + ",");
            }
        }
        return buffer.toString();
    }

    /**
     * 随机生机蓝球一个
     */
    private void randomBlueBall() {
        blueBall = new int[Integer.parseInt(mBlueNumEdit.getText())];
        int index = 0;
        int num = 0;
        boolean con = false;
        while (index < blueBall.length) {
            num = BLUEBALL[randomInt(BLUEBALL.length)];
            //if (blueBall > 0 && num != blueBall) {
            con = isInNum(blueBall, num);
            if (con) {
                blueBall[index] = num;
                index++;
            }
            //}
        }
        Arrays.sort(blueBall);
    }

    private void randomRedBall() {
        redBall = new int[Integer.parseInt(mRedNumEdit.getText())];
        int index = 0;
        int num = 0;
        boolean con = false;
        while (index < redBall.length) {
            num = REDBALL[randomInt(REDBALL.length)];
            //if (blueBall > 0 && num != blueBall) {
            con = isInNum(redBall, num);
            if (con) {
                redBall[index] = num;
                index++;
            }
            //}
        }
        Arrays.sort(redBall);
    }

    /**
     * 在一定范围内生成一个随机数
     *
     * @param range
     * @return
     */
    private int randomInt(int range) {
        int index = 0;
        do {
            final Random random = new Random();
            index = random.nextInt(range);
        } while (index < 0);
        return index;
    }

    /**
     * 判断指定的数组里是否包含指定的数字
     *
     * @param array
     * @param num
     * @return
     */
    private static boolean isInNum(int[] array, int num) {
        for (int n : array) {
            if (num == n)
                return false;
        }
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
